﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ETWJ.DTO;
using ETWJ.Iservice;
using ETWJ.Model;
using Microsoft.AspNetCore.Mvc;

namespace ETWJ.ERP.Controllers
{
    public class CompanyController : Controller
    {
        public ICompanyService<Company ,CompanyDTO> companyService { get; set; }
        public CompanyController(ICompanyService<Company, CompanyDTO> companyService)
        {
            this.companyService = companyService;
        }


        public async Task<IActionResult> BatchDeleted(string ids)
        {
            string[] empIds = ids.Split(',');
            List<int> deletedResult = new List<int>();
            List<int> deletedResultIds = new List<int>();
            foreach (var id in empIds)
            {
                var data = await companyService.GetByIdAsync(Convert.ToInt32(id));
                if (data != null)
                {
                    var result = await companyService.MarkDeleted(data.Id);
                    deletedResult.Add(result);
                    deletedResultIds.Add(data.Id);
                }
            }

            if (deletedResult.Contains(-1))
            {
                string deletedFaillids = "";
                foreach (var id in deletedResultIds)
                {
                    deletedFaillids += id + "-";
                }
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Company/List", msg = "删除失败！删除失败的编号为：" + deletedFaillids, isSuccess = "OK" });
            }

            return RedirectToAction("Tip", "Home", new { redirectUrl = "/Company/List", msg = "删除成功！", isSuccess = "OK" });
        }

        [HttpPost]
        public async Task<IActionResult> Edit(CompanyDTO compy)
        {
            var resp=await companyService.GetByIdAsync(compy.Id);
            if (resp == null)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Company/List", msg = "不存在该公司！", isSuccess = "error" });
            }

            var result=await companyService.Update(compy);
            if (result <= 0)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Company/List", msg = "修改失败！", isSuccess = "error" });
            }

            return RedirectToAction("Tip", "Home", new { redirectUrl = "/Company/List", msg = "修改成功！", isSuccess = "ok" });
        }

        public async Task<IActionResult> Edit(int id)
        {
            var resp=await companyService.GetByIdAsync(id);
            if(resp==null)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Company/List", msg = "不存在该公司！", isSuccess = "error" });
            }

            return View(resp);

        }

        public async Task<IActionResult> Add()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Add(Company comp)
        {
            var resp=await companyService.GetCompanyByName(comp.CompanyName);
            if(resp!=null)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Company/List", msg = "已经存在该公司！", isSuccess = "error" });
            }

            var result=await companyService.AddNewAsync(comp);
            if(result<=0)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Company/List", msg = "添加失败！", isSuccess = "error" });
            }

            return RedirectToAction("Tip", "Home", new { redirectUrl = "/Company/List", msg = "添加成功！", isSuccess = "ok" });
        }


        public async Task<IActionResult> List(int pageIndex=1,int count=5)
        {
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }

            var model = await companyService.GetByPage(pageIndex, count);

            var totalCount = await companyService.GetTotalCountAsync();
            var currentPageIndex = pageIndex;

            var pageCount = Math.Ceiling(totalCount / 5.0);

            ViewBag.PageCount = pageCount;
            ViewBag.CurrentPageIndex = currentPageIndex;

            return View(model);
        }

        public IActionResult Index()
        {
            return View();
        }
    }
}